Legato
Legato

GoFiler Legato Script Reference

 

Legato v 1.4j

Application v 5.22b

  

 

Chapter SixFile Functions (continued)

6.9 MIME Encoding

6.9.1 Overview

Multipurpose Internet Mail Extensions, or MIME, is used to encode complex data in the form of exchangeable simple ASCII data. This is used for email, single page web files or any other method were a series of files need to be encoded and carried with a series of parameters.

MIME data can be found as single page web files (MHT), email messages (EML) and many other multiple encoded document formats. It allows for the definition of loose parameters and encoding of various content types ranging from text based items such as HTML to binary files such as JPEGs.

As a side note, a MIME encoded files and a MIME types are not the same thing.

Legato supports a number methods to access MIME encoded information.

6.9.2 Encoding

A MIME file generally contains a header and a series of complex data elements (parts, attachments or files) separated by a boundary layer. Each boundary layer can have its own parameters and encoding methods.

Each attachment or file can be encoded using a number of methods such as Base64 or quoted printable or 7-bit. Attached data or files may or may not have associated properties.

The MIMEGetProperties function allows the header information to be read as a table of parameter names and data.

6.9.3 Header

MIME files will have at least a basic header specifying that the context is MIME encoded and specifying what is known as a boundary layer. For email messages. there are dozens of standard property names and extension properties. Further, the header may contain duplicate property names with differing data as mail relays and mail processors add information to the messages each time it is handled. The MIMEGetProperties function returns the entire decoded header as a table. As such, elements must be searched rather than conventionally using a key name. For example:

string          s1, s2, s3, s4;
handle          hPop, hMIME;
string          id_list[];
int             ms_list[];
string          data[][];
int             sx, ix, count;
int             rc;


AddMessage("POP3 Example:");

hPop = POPConnect("mail.xxxxxxxxxxxx.com", "my user", "my password");
if (IsError(hPop)) {
  rc = GetLastError();
  AddMessage("  FAIL: Failed to open Connection - 0x%08X", rc);
  exit;
  }
AddMessage("  PASS: POPConnect");

// Get Count
count = POPGetMessageCount(hPop);
if (IsError(count)) {
  AddMessage("Failed to Get Message Count - 0x%08X", rc);
  exit;
  }
if (count == 0) {
  AddMessage("    The Inbox is Empty -- STOPPED");
  exit;
  }     

id_list = POPGetMessageIDList(hPop);
ms_list = POPGetMessageSizeList(hPop);

count++;                        // zero-inclusive

// Dump Message Headers to Log
AddMessage("Message List (%d messages):", count - 1);
ix = 1;
while (ix < count) {
  s1 = POPGetMessageHeader(hPop, ix);
  hMIME = MIMEOpen(s1);
  if (IsError(hMIME)) {
    rc = GetLastError();
    s2 = "";
    s3 = "--Header Error--";
    s4 = FormatString("Code: 0x%08X", rc);
    }
  else {
    s2 = ""; s3 = ""; s4 = "";
    data = MIMEGetProperties(hMIME);
    sx = FindInTable(data, "Date");
    if (sx >= 0) { s2 = data[sx][1]; }
    sx = FindInTable(data, "Subject");
    if (sx >= 0) { s3 = data[sx][1]; }
    sx = FindInTable(data, "From");
    if (sx >= 0) { s4 = data[sx][1]; }
    CloseHandle(hMIME);
    }

  s1 = TrailStringAfter(id_list[ix], 42);
  s3 = TrailStringAfter(s3, 32);
  AddMessage(" %3d %-42s %8a bytes  %-33s %-32s %s ", ix, s1, ms_list[ix], s2, s3, s4);
  ix++; 
  }

In the above, the FindInTable function is used to locate the first occurrence of “Date”, “Subject” and “From”.   

6.9.4 Attachments, Files and Data

Multipart data is encoded and may be files, attachments or messages. For the purposes of extraction and management, each multipart item is considered an item. Since a item may be a message or alternate, it may not be associated with a name or other typical file properties.

Each item within the MIME are separated by a content boundary. Items can also contain additional boundaries as marked by the content-type “multipart/alternative”. Data for each content is also available as an individual item. The MIMEEnumerateItems function can be used to get a list of items and their index positions. Since an item does not have to have a name, the array can have empty string entries. The properties for a specific item index can be retrieved using the MIMEGetItemProperties function. The content of an item can be extracted to a file using the MIMEItemsToFiles or MIMEItemToFile function, a string using the MIMEItemToString or a Data Object using the MIMEItemToDataObject function.

Content is not necessarily guaranteed to file associated filename. This is particularly true of email messages.

6.9.5 Functions

Object Management:

MIMEOpen — Opens a specified MIME encoded string or file such as an MHT or EML file.

Parameters:

MIMEGetProperties — Retrieves the available properties for a MIME Object in the form of a string table.

Item Data:

MIMEEnumerateItems — Enumerates all the item entries contained in a MIME Object.

MIMEGetItemProperties — Retrieves all available properties for a specified file within a MIME Object.

MIMEItemsToFiles — Extracts all the named items in a MIME Object to files.

MIMEItemToFile — Extracts a specified item from a MIME Object to a file.

MIMEItemToString — Extracts a specified item in a MIME Object to a string.